package com.scut.graduation.controller;

import com.scut.graduation.entity.User;
import com.scut.graduation.entity.vo.LoginVo;
import com.scut.graduation.exception.GraduationManagementException;
import com.scut.graduation.service.UserService;
import com.scut.graduation.util.R;
import com.scut.graduation.util.ResultCode;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2022-05-24 17:36:18
 */
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    @PostMapping("/login")
    public R handleLogin(LoginVo loginVo){
        if (!loginVo.isValidated()) {
            throw new GraduationManagementException(ResultCode.PARAMS_IS_INVALID.getCode(),ResultCode.PARAMS_IS_INVALID.getMessage());
        }
        User user = userService.handleLogin(loginVo);
        return R.ok().data("user",user);
    }
    @PostMapping("/register")
    public R handleRegister(){

        return R.ok();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id
     *            主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public User selectOne(Integer id) {
        return this.userService.queryById(id);
    }

}